package com.example.demo.config;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 验证器，增加了登录次数校验功能
 *
 * @author 徐鑫
 */
public class RetryLimitCredentialsMatcher extends SimpleCredentialsMatcher {
    private static final Logger log = LoggerFactory.getLogger(RetryLimitCredentialsMatcher.class);

    private int maxRetryCount = 5;//登录次数限制

    /**
     * 设置尝试登录次数限制
     */
    void setMaxRetryCount() {
        this.maxRetryCount = 5;
    }

    /**
     * 获取登录次数限制
     *
     * @return 登录次数限制
     */
    public int getMaxRetryCount() {
        return maxRetryCount;
    }

    /**
     * 根据用户提交的令牌和已存信息进行比对
     *
     * @param token 用户提交的令牌
     * @param info  已存信息
     * @return 登录状态
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        String password = String.valueOf((char[]) token.getCredentials());
        //进行密码比对
        return password.equals(info.getCredentials());
    }
}
